<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
var jsonObj = new Object();
$(function(){
	$("#dialogDiv").dialog({
		title:'寝室基本信息维护',
		width:700,
		height:250,
		top:35,
		closed:true,
		madel:true,
		cache:false,
		onClose:function(){
			$("#dialogIframe").attr("src","");
		}
	});
	
	$("#searchButton").click(function(){
		$("#dormitoryList").datagrid({queryParams:getParams()});
	});
	
	$("#empty").click(function(){
		$("#dormitory").textbox('setValue',"");
		$("#studentId").textbox('setValue',"");
		$("#cnName").textbox('setValue',"");
		$("#dormitoryList").datagrid({queryParams:getParams()});
	});
	
	$("#addButton").click(function(){
		$("#dialogIframe").attr("src","dormitoryAdd.action");
		$("#dialogDiv").dialog('open');
	});
	
	$("#editButton").click(function(){
		var checkObj = $("input[name='id']:checked");
		if(checkObj.size() == 1){
			var id = checkObj.val();
			$("#dialogIframe").attr("src","dormitoryEdit.action?id="+id);
			$("#dialogDiv").dialog('open');
		}else{
			$.messager.alert("提示","请选择一条记录");
		}
	});
	
	$("#deleteButton").click(function(){
		var checkObj = $("input[name='id']:checked");
		if(checkObj.size() == 1){
			var id = checkObj.val();
			$.messager.confirm({
				title:'提示',
				msg:'确定删除吗',
				top:35,
				fn:function(r){
					if(r){
						$.ajax({
							type:'POST',
							url:'dormitoryDel.action',
							data:{id:id},
							datatype:'json',
							success:function(msg){
								if(msg.status=="200"){
									$("#dormitoryList").datagrid({queryParams:getParams()});
								}else{
									$.messager.alert("提示","删除失败");
								}
							}
						});
					}
				}
			});
		}else{
			$.messager.alert("提示","请选择一条记录");
		}
	});
	
	$("#dormitoryList").datagrid({
		url:'<c:url value="dormitoryList.action" />',
		queryParams:getParams(),//pagination:true,
		toolbar:'#carCategoryToolbar',
		pagination:true,
		pagePosition:'bottom',
		pageNumber:1,//初始页码
	    pageSize:10,//初始每页显示记录数量
	    pageList:[10, 20, 30, 40], 
	    fitColumns:true,
	    selectOnCheck:false,
	    //sortName: 'studentId',
	    //sortOrder: 'asc',
	    remoteSort: false,
	    singleSelect:false,
	    rownumbers:true,
	    striped:true,
	    columns:[[
	    	{field:'id',title:'',width:10,align:'center',formatter:function(value,row,index){
	    		return '<input name="id" type="checkbox" value="'+value+'" />';
	    	}},
	    	{field:'dormitory',title:'寝室号',width:20,align:'center',sortable:true},
	    	{field:'studentId',title:'学号',width:70,align:'center',sortable:true},
	    	{field:'cnName',title:'姓名',width:70,align:'center'},
	    	{field:'sex',title:'性别',width:20,align:'center'},
	    	{field:'pnoneNumber',title:'电话号码',width:70,align:'center'},
	    	{field:'college',title:'学院',width:70,align:'center'},
	    	{field:'theClass',title:'班级',width:70,align:'center'},
	    ]],
		onClickRow:function(index,row){
			$("input[type=checkbox]").val([row.id]).attr("checked",true);
	    }
	});
	
});

function getParams(){
	if($("#dormitory").val() != "" && $("#dormitory").val() != null){
		jsonObj.dormitory = $("#dormitory").val();
	}else{
		jsonObj.dormitory = null;
	}
	if($("#studentId").val() != "" && $("#studentId") != null){
		jsonObj.studentId = $("#studentId").val();
	}else{
		jsonObj.studentId = null;
	}
	if($("#cnName").val() != "" && $("#cnName").val() != null){
		jsonObj.cnName = $("#cnName").val();
	}else{
		jsonObj.cnName = null;
	}
	if($("#storiedBuilding").val() != "" && $("#storiedBuilding").val() != null){
		jsonObj.storiedBuilding = $("#storiedBuilding").val();
	}else{
		jsonObj.storiedBuilding = null;
	}
	return jsonObj;
}

function closeDialog(){
	$("#dialogDiv").dialog('close');
	$("#dormitoryList").datagrid({queryParams:getParams()});
}